package me.jessyan.mvparms.demo.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DoubleClickUtils;
import com.jess.arms.widget.CustomPopupWindow;

import javax.inject.Inject;
import javax.inject.Named;

import me.jessyan.mvparms.demo.di.component.fragment.DaggerMineComponent;
import me.jessyan.mvparms.demo.di.module.fragment.MineModule;
import me.jessyan.mvparms.demo.mvp.ui.activity.integral.MyExchangeActivity;
import me.jessyan.mvparms.demo.mvp.ui.activity.other.AboutUsActivity;
import me.jessyan.mvparms.demo.mvp.ui.activity.other.BusinessCooperationActivity;
import me.jessyan.mvparms.demo.mvp.contract.fragment.MineContract;
import me.jessyan.mvparms.demo.mvp.presenter.fragment.MinePresenter;

import me.jessyan.mvparms.demo.R;
import me.jessyan.mvparms.demo.mvp.ui.activity.account.AccountManageActivity;
import me.jessyan.mvparms.demo.mvp.ui.activity.auth.HasAndNoAuthActivity;
import me.jessyan.mvparms.demo.mvp.ui.activity.coupon.MyCouponActivity;
import me.jessyan.mvparms.demo.mvp.ui.activity.integral.MineScoreActivity;
import me.jessyan.mvparms.demo.mvp.ui.activity.user.PersonalDataActivity;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * 我的 一级 framgnet
 */
public class MineFragment extends OneLevelBaseFragment<MinePresenter> implements MineContract.View ,CustomPopupWindow.CustomPopupWindowListener ,View.OnClickListener,PopupWindow.OnDismissListener{

    @Inject
    CustomPopupWindow loginOutPop;
    TextView loginOutTv;

    @Inject
    @Named("QRCodePop")
    CustomPopupWindow qrCodePop;
    @Inject
    @Named("sharePop")
    CustomPopupWindow sharePop;




    public static MineFragment newInstance() {
        MineFragment fragment = new MineFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerMineComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .mineModule(new MineModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.home_mine_fragment, container, false);
        loginOutTv =view.findViewById(R.id.login_out_tv);
        TextView authTv = view.findViewById(R.id.auth_tv);
        authTv.setOnClickListener(this);
        loginOutTv.setOnClickListener(this);
        view.findViewById(R.id.personal_data_tv).setOnClickListener(this);
        view.findViewById(R.id.account_manage_tv).setOnClickListener(this);
        view.findViewById(R.id.integral).setOnClickListener(this);
        view.findViewById(R.id.integral_tv).setOnClickListener(this);
        view.findViewById(R.id.discount).setOnClickListener(this);
        view.findViewById(R.id.discount_tv).setOnClickListener(this);
        view.findViewById(R.id.business_tv).setOnClickListener(this);
        view.findViewById(R.id.about_us_tv).setOnClickListener(this);
        view.findViewById(R.id.my_exchange_tv).setOnClickListener(this);
        view.findViewById(R.id.toolbar_iv).setOnClickListener(this);
        view.findViewById(R.id.share_tv).setOnClickListener(this);
        return view;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        loginOutPop.setOnDismissListener(this);
        qrCodePop.setOnDismissListener(this);
        sharePop.setOnDismissListener(this);


    }
    /**
     * 背景颜色
     */
    private void backgroundAlpha(float f) {
        WindowManager.LayoutParams lp =getActivity().getWindow().getAttributes();
        lp.alpha = f;
        getActivity().getWindow().setAttributes(lp);
    }
    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

    @Override
    public MineFragment getFragment() {
        return this;
    }

    @Override
    public void dismissPop() {
        if(loginOutPop!=null&&loginOutPop.isShowing()){
            loginOutPop.dismiss();
        }
        if(qrCodePop!=null&&qrCodePop.isShowing()){
            qrCodePop.dismiss();
        }
        if(sharePop!=null&& sharePop.isShowing()){
            sharePop.dismiss();
        }
    }

    @Override
    public void popBackShare(int i) {
        dismissPop();
        switch (i){
            case 1:
                showMessage("钉钉");
                break;
            case 2:
                showMessage("微信");
                break;
            case 3:
                showMessage("qq");
                break;
            case 4:
                showMessage("更多");
                break;
                default:
        }
    }

    @Override
    public void initPopupView(View contentView) {
        contentView.findViewById(R.id.pop_start_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loginOutPop.dismiss();
            }
        });
        contentView.findViewById(R.id.pop_end_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loginOutPop.dismiss();
            }
        });

    }

    @Override
    public void onClick(View view) {
        if(DoubleClickUtils.getInstance().isDouble(view)){
            return;
        }
        switch (view.getId()){
            case R.id.toolbar_iv:
                qrCodePop.show();
                backgroundAlpha(0.7f);
                break;
            // 我的兑换
            case R.id.my_exchange_tv:
                startActivity(new Intent(getActivity(), MyExchangeActivity.class));
                break;
            case R.id.login_out_tv:
                loginOutPop.show();
                backgroundAlpha(0.7f);
                break;
            case R.id.auth_tv:
                startActivity(new Intent(getActivity(), HasAndNoAuthActivity.class));
                break;
            case R.id.personal_data_tv:
                startActivity(new Intent(getActivity(), PersonalDataActivity.class));
                break;
            case R.id.account_manage_tv:
                startActivity(new Intent(getActivity(), AccountManageActivity.class));
                break;
                // 积分   ，他们点击是一样的效果
            case R.id.integral:
            case R.id.integral_tv:
                startActivity(new Intent(getActivity(),MineScoreActivity.class));
                break;
                //优惠券  ，点击效果一样
            case R.id.discount:
            case R.id.discount_tv:
                startActivity(new Intent(getActivity(),MyCouponActivity.class));
                break;
                //商务合作
            case R.id.business_tv:
                startActivity(new Intent(getActivity(), BusinessCooperationActivity.class));
                break;
                //关于我们
            case R.id.about_us_tv:
                startActivity(new Intent(getActivity(), AboutUsActivity.class));
                break;
                //分享给好友
            case R.id.share_tv:
                sharePop.showBottom();
                backgroundAlpha(0.7f);
                break;
                default:
        }
    }

    @Override
    public void onDismiss() {
        backgroundAlpha(1);
    }
}
